import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/login/Login'
import Home from '@/views/home/Home'
import Hello from '@/views/hello/Hello'
import ArticleManager from '@/views/article-manager/ArticleManager'
import Discuss from '@/views/discuss/Discuss'
import User from '@/views/user/User'
import { getToken } from "@/utils/auth";
import ArticleUpdate from '@/views/article-manager/ArticleUpdate'
import CreateArticle from '@/views/article-manager/CreateArticle'
import CreateUser from '@/views/user/CreateUser'
import ArticleType from '@/views/article-manager/ArticleType'
import CreateArticleType from '@/views/article-manager/CreateArticleType'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: 'Home',
      component: Home,
      children: [
        {
          path: '/home',
          name: 'Hello',
          component: Hello
        },
        {
          path: '/article-manager',
          name: 'ArticleManager',
          component: ArticleManager
        },
        {
          path: '/discuss',
          name: 'Discuss',
          component: Discuss
        },
        {
          path: '/user',
          name: 'User',
          component: User
        },
        {
          path: '/articleUpdate',
          name: 'ArticleUpdate',
          component: ArticleUpdate
        },
        {
          path: '/createArticle',
          name: 'CreateArticle',
          component: CreateArticle
        },
        {
          path: '/createUser',
          name: 'CreateUser',
          component: CreateUser
        },
        {
          path: '/articleType',
          name: 'ArticleType',
          component: ArticleType
        },
        {
          path: '/createArticleType',
          name: 'CreateArticleType',
          component: CreateArticleType
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  //检查用户是否登陆
  let isLogin = getToken();
  //console.log("检查用户权限 ./." + isLogin);
  //console.log(to);
  if (isLogin) {
    next();
  } else {
    if (to.fullPath == "/Index") { //进行第一个欢迎页面直接放行
      //这是欢迎页面，直接放行
      next();
    } else if (to.fullPath == "/Login") { //登陆页面也直接放行
      //如果是登陆页面，并且用户没有登陆，则跳转到登陆页面
      next();
    } else if (to.fullPath === '/') {
      next();
    } else {
      //跳转到登陆页面
      next('/');
    }
  }
})

export default router